home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / example / HrefButtonArea.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  5.3 KB  |  166 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. /*
  18.  * @(#)HrefButtonArea.java    1.3 95/10/13  
  19.  *
  20.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  21.  *
  22.  * Permission to use, copy, modify, and distribute this software
  23.  * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  24.  * without fee is hereby granted. 
  25.  * Please refer to the file http://java.sun.com/copy_trademarks.html
  26.  * for further important copyright and trademark information and to
  27.  * http://java.sun.com/licensing.html for further important licensing
  28.  * information for the Java (tm) Technology.
  29.  * 
  30.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  31.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  32.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  33.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  34.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  35.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  36.  * 
  37.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  38.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  39.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  40.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  41.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  42.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  43.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  44.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  45.  * HIGH RISK ACTIVITIES.
  46.  */
  47.  
  48. import java.awt.Graphics;
  49. import java.awt.Image;
  50. import java.net.URL;
  51. import java.net.MalformedURLException;
  52.  
  53. /**
  54.  * An improved "Fetch a URL" ImageArea class.
  55.  * This class extends the basic ImageArea Class to fetch a URL when
  56.  * the user clicks in the area.  In addition, special custom highlights
  57.  * are used to make the area look and feel like a 3-D button.
  58.  *
  59.  * @author     Jim Graham
  60.  * @version     1.3, 13 Oct 1995
  61.  */
  62. class HrefButtonArea extends ImageMapArea {
  63.     /** The URL to be fetched when the user clicks on this area. */
  64.     URL anchor;
  65.     /** The highlight image for when the button is "UP". */
  66.     Image upImage;
  67.     /** The highlight image for when the button is "DOWN". */
  68.     Image downImage;
  69.     /** This flag indicates if the "button" is currently pressed. */
  70.     boolean pressed = false;
  71.     /** The border size for the 3-D effect. */
  72.     int border = 5;
  73.  
  74.     /**
  75.      * The argument string is the URL to be fetched.
  76.      * This method also constructs the various highlight images needed
  77.      * to achieve the 3-D effect.
  78.      */
  79.     public void handleArg(String arg) {
  80.     try {
  81.         anchor = new URL(parent.getDocumentBase(), arg);
  82.     } catch (MalformedURLException e) {
  83.         anchor = null;
  84.     }
  85.     if (border * 2 > W || border * 2 > H) {
  86.         border = Math.min(W, H) / 2;
  87.     }
  88.     }
  89.  
  90.     public void makeImages() {
  91.     upImage = parent.getHighlight(X, Y, W, H,
  92.                       new ButtonFilter(false,
  93.                                parent.hlpercent,
  94.                                border, W, H));
  95.     downImage = parent.getHighlight(X, Y, W, H,
  96.                     new ButtonFilter(true,
  97.                              parent.hlpercent,
  98.                              border, W, H));
  99.     }
  100.  
  101.     public boolean imageUpdate(Image img, int infoflags,
  102.                    int x, int y, int width, int height) {
  103.     if (img == (pressed ? downImage : upImage)) {
  104.         return parent.imageUpdate(img, infoflags, x + X, y + Y,
  105.                       width, height);
  106.     } else {
  107.         return (img == downImage || img == upImage);
  108.     }
  109.     }
  110.  
  111.     /**
  112.      * The isTerminal method indicates whether events should propagate
  113.      * to the areas underlying this one.
  114.      */
  115.     public boolean isTerminal() {
  116.     return true;
  117.     }
  118.  
  119.     /**
  120.      * The status message area is updated to show the destination URL.
  121.      * The graphical highlight is achieved using the ButtonFilter.
  122.      */
  123.     public void highlight(Graphics g) {
  124.     if (entered) {
  125.         g.drawImage(pressed ? downImage : upImage, X, Y, this);
  126.     }
  127.     }
  128.  
  129.     public boolean enter() {
  130.     showStatus((anchor != null)
  131.            ? "Go To " + anchor.toExternalForm()
  132.            : null);
  133.     repaint();
  134.     return true;
  135.     }
  136.  
  137.     public void exit() {
  138.     showStatus(null);
  139.     repaint();
  140.     }
  141.  
  142.     /**
  143.      * Since the highlight changes when the button is pressed, we need
  144.      * to record the "pressed" state and induce a repaint.
  145.      */
  146.     public boolean press() {
  147.     pressed = true;
  148.     repaint();
  149.     return true;
  150.     }
  151.  
  152.     /**
  153.      * The new URL is fetched when the user releases the mouse button
  154.      * only if they are still in the area.
  155.      */
  156.     public boolean lift(int x, int y) {
  157.     pressed = false;
  158.     repaint();
  159.     if (inside(x, y) && anchor != null) {
  160.         showDocument(anchor);
  161.     }
  162.     return true;
  163.     }
  164. }
  165.  
  166.